/*******************************************************************************
 ** Name: Client.cpp                                          **
 ** Description:                                                              **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved License                             **
 **                                                                           **
 **  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
 **  All rights reserved.                                                     **
 **                                                                           **
 **  Redistribution and use in source and binary forms, with or without       **
 **  modification, are permitted provided that the following conditions are   **
 **  met:                                                                     **
 **                                                                           **
 **  * Redistributions of source code must retain the above copyright notice, **
 **    this list of conditions and the following disclaimer.                  **
 **  * Redistributions in binary form must reproduce the above copyright      **
 **    notice, this list of conditions and the following disclaimer in the    **
 **    documentation and/or other materials provided with the distribution.   **
 **                                                                           **
 **  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      **
 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED **
 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           **
 ** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT        **
 ** HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,    **
 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED  **
 ** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR    **
 ** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    **
 ** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      **
 ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        **
 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              **
 **                                                                           **
 ******************************************************************************/
// Created on March 24, 2010, 12:07 AM
// Modified $Date: 2011-09-18 21:48:59 +0000 (Sun, 18 Sep 2011) $ by $Author: gian.james $
#define  Client_cpp_ID    "$Id: client.cpp 145 2011-09-18 21:48:59Z gian.james $"

extern "C" {
#include <stdlib.h>
#include <stdarg.h>
}
#include <iostream>
#include <sstream>
#include "client.hpp"
#include <map>
#include <string>
#include "ansi.hpp"


extern std::map<std::string, std::string> cfgSettings;

//struct sock { };
//struct sname { };
//typedef multi_index_container<
//	Client,
//	indexed_by<
//		ordered_unique<tag<sock>, BOOST_MULTI_INDEX_MEMBER(Client,int,sock)>,
//		ordered_unique<tag<sname>, BOOST_MULTI_INDEX_MEMBER(Client,std::string,sname)>
//	>
//> clientSet;
//typedef clientSet::index<sock>::type clientSetBySocket;
//typedef clientSet::index<sname>::type clientSetByName;

clientSet clients;

Client * FindClient(int s) {
	clientSetBySocket::iterator it = clients.get<sock>().find(s);
	return (*it);
}
Client * FindClient(std::string &n) {
	clientSetByName::iterator it = clients.get<sname>().find(n);
	return (*it);
}

// TODO This has no ANSI coloring or any formatting
int
Client::Tell(std::string from, char * msg, ...)
{
	Client * c = FindClient(from);

	if (!BMCHK(player->Agent()->Flags(),CHAR_ALLOW_TELL))
	{
		if (c->Player()->Agent()->IsGod() || c->Player()->Agent()->IsHandler()
				|| c->Player()->Agent()->IsControl())
		{
			va_list	ap;
			va_start(ap,msg);
			char buff[MAX_RECV_LEN];

			vsprintf(buff,msg,ap);

			std::stringstream ss;
			if (c->Player()->Agent()->IsGod())
				ss << ANSI_BLUE << "GOD Speaks: " << ANSI_NORMAL;
			else if (c->Player()->Agent()->IsHandler())
				ss << ANSI_YELLOW << "Agent Handler " << ANSI_NORMAL;
			else if (c->Player()->Agent()->IsControl())
				ss << ANSI_ORANGE << "Agency Controller " << ANSI_NORMAL;
			else if (c->Player()->Agent()->IsDirector())
				ss << ANSI_GREEN << "Director " << ANSI_NORMAL;
			ss << from << " tells you \"" << ANSI_WHITE << buff << ANSI_NORMAL << "\"" << std::endl;

			va_end(ap);
		}
		return 0;
	}
	va_list	ap;
	va_start(ap,msg);
	char buff[MAX_RECV_LEN];

	vsprintf(buff,msg,ap);

	std::stringstream ss;

	if (c->Player()->Agent()->IsGod())
		ss << ANSI_BLUE << "GOD Speaks: " << ANSI_NORMAL;
	else if (c->Player()->Agent()->IsHandler())
		ss << ANSI_YELLOW << "Agent Handler " << ANSI_NORMAL;
	else if (c->Player()->Agent()->IsControl())
		ss << ANSI_ORANGE << "Agency Controller " << ANSI_NORMAL;
	else if (c->Player()->Agent()->IsDirector())
		ss << ANSI_GREEN << "Director " << ANSI_NORMAL;
	ss << from << " tells you \"" << ANSI_WHITE << buff << ANSI_NORMAL << "\"" << std::endl;

	va_end(ap);

	Socket::WriteLine(sock,ss.str().data());

	return 1;
}

// TODO
int
Client::Say(std::string from, char * msg, ...)
{

	return 0;
}

int
Client::Shout(std::string from, char * msg, ...)
{

	return 0;
}

int
Client::Wall(std::string from, char * msg, ...)
{

	return 0;
}

int
Client::Wallop(std::string from, char * msg, ...)
{

	return 0;
}

int
Client::System(std::string from, char * msg, ...)
{
	va_list	ap;
	va_start(ap,msg);
	char buff[MAX_RECV_LEN];

	vsprintf(buff,msg,ap);



	return 0;
}



void
Client::SocketAddress(sockaddr_in & s)
{
	memset(&saddr,0,sizeof(saddr));
	saddr.sin_family	= s.sin_family;
	saddr.sin_addr.s_addr = s.sin_addr.s_addr;
	saddr.sin_port		= s.sin_port;
}

Client::Client(int s)
: WorldObject(OType::Client,UserName())
{
    sock = s;
    loginTime = time(NULL);
    lastRecv = loginTime;
    quota	= atoi(cfgSettings["default_quota"].data());
    flags = 0x0000;
    BMSET(flags,CLIENT_I3_ENA|CLIENT_COLOR_ENA);
    ibuff = new std::ringbuffer<std::string>(atoi(cfgSettings["client_buff_max"].data()));
    obuff = new std::ringbuffer<std::string>(atoi(cfgSettings["client_buff_max"].data()));
    player = NULL;
}

Client::Client(const std::string & n)
: WorldObject(OType::Client,n)
{
	sock = 0;
	loginTime = time(NULL);
    lastRecv = loginTime;
    quota	= atoi(cfgSettings["default_quota"].data());
    flags = 0x0000;
    BMSET(flags,CLIENT_I3_ENA|CLIENT_COLOR_ENA);
    ibuff = new std::ringbuffer<std::string>(atoi(cfgSettings["client_buff_max"].data()));
    obuff = new std::ringbuffer<std::string>(atoi(cfgSettings["client_buff_max"].data()));
    player = NULL;
}

Client::Client(const Client& orig)
: WorldObject(OType::Client, UserName())
{
    sock = orig.sock;
    sname = orig.sname;
    loginTime = orig.loginTime;
    lastRecv = orig.lastRecv;
    quota = orig.quota;
    flags = orig.flags;
}

Client::~Client()
{
	if (player) {
		// TODO log logout time, save variables, etc.
		delete player;
	}
}

